<template>
  <div class="otherWarehousingList">
    <jc-button
      :btn-arr="['reload', bomCost('insertStkOtherinstockDetails') && 'newy', 'batchFill', 'remove', 'sub', 'option']"
      :disab-arr="['close', 'cancel', 'using', 'copy', 'freeze']"
      :fbillno="fbillNo"
      url="tStkOtherinstock/queryStkInstockList"
      @refresh="handleGetTable"
      @newly="newAssem"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @toExcel="handleExport"
      @del="delectHandle"
      @handleCom1="handleCom1(1)"
      @handleCom2="handleCom1(2)"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleSearch"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleSearch">
          {{ $t('btn.search') }}
        </el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleSearch"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        sortable
        table-selection
        :get-summaries="getSummaries"
        show-summary
        @selectionChange="handleSelectAll"
        @clickRow="handleInto"
      >
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="handleInto(col.scope.row)">
            {{ col.scope.row.fbillNo }}
          </el-link>
        </template>
        <!--    物料编码    -->
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
        <!--审核状态-->
        <template v-slot:btnStates="col">
          <span v-if="col.scope.row.fdocumentstatus === ''" />
          <el-tag
            v-else
            :type="col.scope.row.stateColor"
          >
            {{ col.scope.row.fdocumentstatus }}
          </el-tag>
        </template>
        <!--关闭状态-->
        <template v-slot:btnState="col">
          <el-tag
            v-if="col.scope.row.fclosestatus === 'A'"
          >
            {{ $t('market.normal') }}
          </el-tag>
          <el-tag v-else type="danger">{{ $t('market.closed') }}</el-tag>
        </template>
      </jc-table>
      <cardTab @search="handleQueryBomList" />
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="handleGetTable"
      />
    </div>
  </div>
</template>

<script>
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import {
  queryStkInstockList, delStkOtherinstocks,
  notsubmitStkOtherinstock, submitStkOtherinstock,
  unauditStkOtherinstocks, auditStkOtherinstocks
} from '@/api/repertory/picking/otherWarehousing'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import { CloseId, toMxBmina } from '@/components/ToMxamineState'
import jcButton from '@/components/Button'

export default {
  name: 'OtherWarehousingList',
  components: {
    cardTab: () => import('@/components/cardTab'),
    search,
    filtration,
    jcButton
  },
  mixins: [searData],
  data() {
    return {
      ftype: 33, // 其他出入库模块
      fbillNo: 'fbillno', // 编码
      total: 0, // 总条目
      pageNum: 1, // 当前页
      pageSize: 200, // 每页显示多少条数据
      // 表头
      tableHeader: [
        { label: this.$t('market.billNo'), prop: 'fbillNo', type: 'tag', width: '130px' },
        { label: this.$t('market.placedate'), prop: 'fdate', width: '100px' },
        { label: this.$t('market.documentstatus'), type: 'states', prop: 'fdocumentstatus', width: '100px' },
        { label: this.$t('market.closeState'), type: 'state', prop: 'fclosestatus', width: '80' },
        { label: this.$t('market.billType'), prop: 'fbillTypeName', width: '120' },
        { label: this.$t('market.fqty'), prop: 'fqty', width: '80', align: 'right' },
        { label: this.$t('market.note'), prop: 'fremark' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', minWidth: '100px' },
        { label: this.$t('market.fmaterial'), prop: 'fnumber', type: 'order', width: '120px' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '200px' },
        { label: this.$t('market.specificaTion'), prop: 'fspecificaTion', minWidth: '100px' },
        { label: this.$t('market.funit'), prop: 'funitName', width: '80px' },
        { label: this.$t('market.stock2'), prop: 'fstockName', minWidth: '100px' },
        { label: this.$t('market.deaprt'), prop: 'fdeptName' },
        { label: this.$t('market.creator'), prop: 'fcreator', width: '100' }
      ],
      tableHeight: '72vh',
      // 表格数据
      tableData: [],
      listDate: [],
      tableHeader1: [
        { label: this.$t('market.stocker'), prop: 'fstockerName' },
        { label: this.$t('market.billType'), prop: 'fbillTypeName' },
        { label: this.$t('market.valueOf'), prop: 'fworth' },
        { label: this.$t('market.note2'), prop: 'fentryremark' },
        { label: this.$t('market.createDate'), prop: 'fcreatedate' },
        { label: this.$t('market.approver'), prop: 'fapprover' },
        { label: this.$t('market.approveDate'), prop: 'fapprovedate' },
        { label: this.$t('market.modifier'), prop: 'fmodifier' },
        { label: this.$t('market.modifyDate'), prop: 'fmodifydate' },
        { label: this.$t('market.closeor'), prop: 'fcloser' },
        { label: this.$t('market.closeyDate'), prop: 'fclosedate' }
      ],
      selectAll: [] // 全选数据
    }
  },
  created() {
    this.handleGetTable()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 240px)' : '72vh'
  },
  methods: {
    // 导出表格-其它入库单
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.selectAll && this.selectAll.length > 0) {
          const list = this.selectAll.map(item => {
            return (toMxBmina(item), CloseId(item))
          })
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '其它入库单列表')
        } else {
          queryStkInstockList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list.map(item => {
                return (toMxBmina(item), CloseId(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '其它入库单列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取列表数据
    async handleGetTable(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const res = await queryStkInstockList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        search: this.searCollData.concat(this.details)
      })
      const { code, data, message } = res
      if (code !== 0) {
        this.$message.warning(message)
        return
      }
      this.tableData = data.list.map(item => {
        return toMxBmina(item)
      })
      this.total = data.total
      this.loading = false
    },
    // 成品 原材料
    handleQueryBomList(ev) {
      this.searCollData = []
      this.pageNum = 1
      this.searCollData = ev
      this.searchSon = ev
      this.handleGetTable()
    },
    // 全选
    handleSelectAll(item) {
      this.selectAll = item
      this.fbillNo = item[0].fbillNo
      this.listDate = []
      item.forEach(item => {
        item.fbillNo && this.listDate.push(item.fbillNo)
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.listDate }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetTable()
        this.listDate = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? submitStkOtherinstock : notsubmitStkOtherinstock
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? auditStkOtherinstocks : unauditStkOtherinstocks
      this.toExamineFun(api)
    },
    // 删除
    delectHandle() {
      this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {
        const api = delStkOtherinstocks
        this.toExamineFun(api)
      })
    },
    // 新增入库单
    newAssem() {
      this.$router.push({ path: '/otherWarehousingAdd' })
    },
    // 进入详情
    handleInto(item) {
      this.$router.push({ path: `/otherWarehousingDetails/${item.fid}` })
    },
    // 搜索
    handleSearch(ev) {
      this.pageNum = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetTable(ev.detail)
      } else {
        this.handleGetTable()
      }
    },
    // 套打
    handleCom1(ev) {
      const id = this.listDate.join(',')
      if (ev === 1) {
        this.$router.push({
          path: `/otherWarehousTong1/${id}`
        })
      } else {
        this.$router.push({
          path: `/otherWarehousLian1/${id}`
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.otherWarehousingList {
  @include listBom;
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
.success{
  color: #67c23a;
}
.warning{
  color: #e6c23c;
}
.fbillno {
  color: #409eff;
  &:hover {
    text-decoration: underline;
    cursor: pointer;
  }
}
</style>
